package shapes.shapes2D.distribution.modulo 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import samples.BaseSample;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 * 
	 * 
	 * http://pixelero.wordpress.com/2008/07/28/various-functions-with-modulo/
	 */
	public class Modulo extends BaseSample
	{
		
		private var operation:int = 0;
		private var totalOperations:int = 11;
		private var txt:TextField;
		private var tf:TextFormat;
		private var bd:BitmapData;
		
		public function Modulo(  ) 
		{
			
			
			x = 100;
			y =  sh / 2 - 50;
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, nextOperation );
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			tf = new TextFormat( 'Verdana', 24, 0xFFFFFF );
			txt.y = 200;
			
			bd = new BitmapData( sw - 200, 1, false, 0 );
			var bmp:Bitmap = new Bitmap( bd );
			bmp.height = 30;
			bmp.y = 250;
			addChild( bmp );
			nextOperation( null );
			
		}
		
		private function nextOperation(e:MouseEvent):void 
		{
			graphics.clear();
			graphics.lineStyle( 0, color );
			bd.fillRect( bd.rect, 0 );
			var y:Number = 0;
			var m:Number = 25;
			var description:String  = '';
			for ( var x:int = 0; x < sw - 200; x++ )
			{
				
				switch( operation )
				{
					case 0:
						description = '( x % 100 )';
						y = ( x % 100 );
					break;
					
					case 1:
						description = '( x % 100 + x % 79 ) * .5';
						y = ( x % 100 + x % 79 ) * .5;
					break;
					
					case 2:
						description = '(x % 100 + x % 79 + x % 31 + x % 17) * .25';
						y = (x % 100 + x % 79 + x % 31 + x % 17) * .25;
					break;
					
					case 3:
						description = 'x % 50 -x % 30';
						y = x % 50 -x % 30;
					break;
					
					case 4:
						description = 'x % 50 -x % 30 +x % 20 -x % 12';
						y = x % 50 -x % 30 +x % 20 -x % 12;
					break;
					
					case 5:
						description = 'Math.cos( x / 180 ) * 100 % x';
						y = Math.cos( x / 180 ) * 100 % x;
					break;
					
					case 6:
						description = 'zigzag( x, m )';
						y = zigzag( x, m );
					break;
					
					case 7:
						description = 'zigzag( x, 25 ) + zigzag( x, 15 )';
						y = zigzag( x, 25 ) + zigzag( x, 15 );
					break;
					
					case 8:
						description = 'zigzag(x, 25) +zigzag(x, 21) + zigzag(x, 15)';
						y = zigzag(x, 25) +zigzag(x, 21) + zigzag(x, 15);
					break;
					
					case 9:
						description = '( x * x ) % ( 100 - Math.sin( y / 180 ) * 50 )';
						y = ( x * x ) % ( 100 - Math.sin( y / 180 ) * 50 );
					break;
					
					case 10:
						description = '(x * x * x) % 150 - (x * x * x) % 75';
						y = (x * x * x) % 150 - (x * x * x) % 75;
					break;
					
				}
				graphics.lineTo( x,100-y );
				bd.setPixel( x, 0, y << 16 | y << 8 | y );
			}
			txt.text ='operation: y = ' + description;
			txt.setTextFormat( tf );
			graphics.drawRect( 0, -100, sw - 200, 300 );
			
			
			
			operation++;
			operation %= totalOperations;
			
		}
		
		private function zigzag(x:Number, m:Number):Number
		{
			return Math.abs(x % (2 * m) - m);
			
		}
	}

}